import { getDept, addDept, updateDept, getCurrentUsedOrderNum, queryTree } from '@/api/system/dept'
import AntModal from '@/components/pt/dialog/AntModal'
import SelectDept from '@/components/pt/selectDept/SelectDept'
export default {
  name: 'CreateForm',
  props: {
    deptOptions: {
      type: Array,
      required: true
    },
    expandedKeys: {
      type: Array,
      required: true
    }
  },
  components: {
    AntModal,
    SelectDept
  },
  data () {
    const validateParentId = (rule, value, callback) => {
      if (value === undefined || value === '' || value === null) {
        callback(new Error('上级部门不允许为空'))
      } else {
        callback()
      }
    }
    return {
      loading: false,
      parentIdShow: false,
      hasChild: false,
      selectScope: 'all',
      deptTypeEnableValue: [],
      formTitle: '',
      currentRow: undefined,
      oldParentId: '',
      spinning: false,
      delayTime: 200,
      customStyle: 'background: #fff;ssborder-radius: 4px;margin-bottom: 24px;border: 0;overflow: hidden',
      parent: {},
      // 表单参数
      form: {
        id: undefined,
        parentId: undefined,
        name: undefined,
        orderNum: 0
      },
      open: false,
      rules: {
        parentId: [{ required: true, message: '上级部门不能为空', trigger: 'blur', validator: validateParentId }],
        name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
        orderNum: [{ required: true, message: '排序不能为空', trigger: 'blur' }]
      }
    }
  },
  filters: {
  },
  created () {
  },
  computed: {
  },
  watch: {
  },
  methods: {
    onSelectDept (result) {
      console.log(result)
      this.parent = result
      this.form.parentId = result.deptIds
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.$emit('close')
    },
    // 表单重置
    reset () {
      if (this.$refs.form !== undefined) {
        this.$refs.form.resetFields()
      }
    },
     /** 新增按钮操作  */
    handleAdd (row) {
      this.parentIdShow = true
      this.oldParentId = ''
      if (row !== undefined) {
        this.currentRow = row
        this.oldParentId = row.deptId
        this.form.parentId = { ids: row.deptId, names: row.name }
      }
      /** 获取最大编号 */
      getCurrentUsedOrderNum(row !== undefined ? row.deptId : '0').then(response => {
        this.form.orderNum = response.data
      })
      this.$emit('getTreeselect')
      this.formTitle = '添加部门'
      this.open = true
    },
    setNodeData (data) {
      console.log(data)
      this.currentRow.name = data.name
      this.currentRow.deptId = data.deptId
      this.currentRow.orderNum = data.orderNum
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      var that = this
      that.currentRow = row
      that.open = true
      that.formTitle = '修改部门'
      that.hasChild = row.children.length > 0
      that.spinning = !that.spinning
      getDept(row.deptId).then(response => {
        that.oldParentId = response.data.parentId
        that.form = response.data
        if (response.data.parentId !== '0') {
          that.parentIdShow = true
        } else {
          that.parentIdShow = false
        }
        that.parent = { deptIds: response.data.parentId, names: response.data.parentName }
        console.log(that.parent)
        that.spinning = !that.spinning
      })
       this.$emit('getTreeselect', row)
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate(valid => {
        if (valid) {
          const saveForm = JSON.parse(JSON.stringify(this.form))
          if (this.form.deptId !== undefined) {
            updateDept(saveForm).then(response => {
              this.$message.success('修改成功', 3)
              this.$emit('ok')
              this.cancel()
            })
          } else {
            addDept(saveForm).then(response => {
              this.$message.success('新增成功', 3)
              this.$emit('ok')
              this.cancel()
            })
          }
        } else {
          return false
        }
      })
    },
    onLoadData (treeNode) {
      return new Promise(resolve => {
        if (treeNode.dataRef.children) {
          resolve()
          return
        }
        queryTree({
          deptId: treeNode.dataRef.deptId
        }).then(response => {
            treeNode.dataRef.children = response.data
            resolve()
        })
      })
    }
  }
}
